package com.azt.data.utils;

import com.azt.api.pojo.Admin;
import com.azt.api.pojo.DataFunction;
import com.xiaoleilu.hutool.util.CollectionUtil;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * 登录权限相关相关
 */
public class ContextUtils {

	/**
	 * SpringMvc下获取request response session
	 *
	 * @return
	 */
	public static HttpServletRequest getRequest() {
		return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
	}

	public static HttpServletResponse getResponse() {
		return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
	}

	public static HttpSession getSession() {
		HttpSession session = getRequest().getSession();
		return session;

	}

	public static Admin getAdmin() {
		Admin admin = (Admin) getSession().getAttribute(Globals.LOGIN_ADMIN);
		return admin;
	}

	public static Admin setAdmin(Admin admin) {
		getSession().setAttribute(Globals.LOGIN_ADMIN, admin);
		return admin;
	}
	public static void setFunctions(List<DataFunction> functions) {
		getSession().setAttribute(Globals.ROLE_FUNCTION, functions);
	}

	public static List<DataFunction> getFunctions() {
		List<DataFunction> functions = (ArrayList<DataFunction>)getSession().getAttribute(Globals.ROLE_FUNCTION);
		return functions;
	}

	public static void setUris(List<String> uris) {
		getSession().setAttribute(Globals.ROLE_FUNCTION_URI, uris);
	}

	public static List<String> getUris() {
		List<String> uris = (ArrayList<String>)getSession().getAttribute(Globals.ROLE_FUNCTION_URI);
		return uris;
	}

	/**
	 * 菜单权限判断
	 * @param uri
	 * @return
	 */
	public static boolean checkFunctionPermission(String uri) {
		List<String> uris = getUris();
		return uris.contains(uri);
	}

	public static void loginOut() {
		getSession().invalidate();
	}

}
